//
//  Main.cpp
//  BaseDemo
//
//  Created by jiuyv on 2019/5/8.
//  Copyright © 2019 Julija. All rights reserved.
//

#include "GLTools.h"
#include "GLShaderManager.h"
#include "GLFrustum.h"
#include "GLBatch.h"
#include "GLMatrixStack.h"
#include "GLGeometryTransform.h"
#include "StopWatch.h"

#include <math.h>

#include <glut/glut.h>
#include <stdio.h>



GLShaderManager     shaderManager;      //着色器管理器
GLMatrixStack       modelViewMatrix;    //模型视图矩阵堆栈
GLMatrixStack       projectionMatrix;   //投影视图堆栈
GLFrustum           viewFrustum;        //视景体
GLGeometryTransform transformPipeline;  //几何图形变换管道

GLTriangleBatch     torusBatch;         //大球
GLTriangleBatch     sphereBatch;        //小球

GLBatch             floorBatch;         //地板

GLFrame             cameraFrame;        //角色帧 照相机角色帧

//添加随机球
#define NUM_SPHERES 50
GLFrame spheres[NUM_SPHERES];



void ChangeSize(int w, int h){
    //设置视口
    glViewport(0, 0, w , h);
    
    //创建投影矩阵
    viewFrustum.SetPerspective(35.0f, float(w)/float(h), 1.0f, 100.0f);
    
    //获取投影矩阵并将其加载到投影矩阵堆栈上
    projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
    
    transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
    
}

void SetupRC(){
    
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    shaderManager.InitializeStockShaders();
    
    //开启深度测试
    glEnable(GL_DEPTH_TEST);
    
    //设置地板顶点数据
    floorBatch.Begin(GL_LINES, 324);
    for (GLfloat x = -20.0; x <= 20.0f; x+= 0.5) {
        floorBatch.Vertex3f(x, -0.55f, 20.0f);
        floorBatch.Vertex3f(x, -0.55f, -20.0f);
        
        floorBatch.Vertex3f(20.0f, -0.55f, x);
        floorBatch.Vertex3f(-20.0f, -0.55f, x);
    }
    floorBatch.End();
    
    //设置大球模型
    gltMakeSphere(torusBatch, 0.4f, 40, 80);
    
    //设置小球模型
    gltMakeSphere(sphereBatch, 0.1f, 26, 13);
    
    //随机位置放置小球球
    for (int i = 0; i < NUM_SPHERES; i++) {
        
        //y轴不变，X,Z产生随机值
        GLfloat x = ((GLfloat)((rand() % 400) - 200 ) * 0.1f);
        GLfloat z = ((GLfloat)((rand() % 400) - 200 ) * 0.1f);
        
        //在y方向，将球体设置为0.0的位置，这使得它们看起来是飘浮在眼睛的高度
        //对spheres数组中的每一个顶点，设置顶点数据
        spheres[i].SetOrigin(x, 0.0f, z);
    }
    
}

void RenderScene(void){
    
    //颜色值
    static GLfloat vFloorColor[] = { 0.0f, 1.0f, 0.0f, 1.0f};
    static GLfloat vTorusColor[] = { 1.0f, 0.0f, 0.0f, 1.0f};
    static GLfloat vSphereColor[] = { 0.0f, 0.0f, 1.0f, 1.0f};
    
    //基于时间动画
    static CStopWatch roTimer;
    float yRot = roTimer.GetElapsedSeconds() * 60.0f;
    
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    //加入观察者
    M3DMatrix44f mCamera;
    cameraFrame.GetCameraMatrix(mCamera);
    modelViewMatrix.PushMatrix(mCamera);
    
    //绘制地面
    shaderManager.UseStockShader(GLT_SHADER_FLAT,transformPipeline.GetModelViewProjectionMatrix(),vFloorColor);
    floorBatch.Draw();
    
    //获取光源位置
    M3DVector4f vLightPos = {0.0f,10.0f,5.0f,1.0f};
    
    //使大球位置向屏幕里平移3.0
    modelViewMatrix.Translate(0.0f, 0.0f, -3.0f);
    
    //压栈(复制栈顶)
    modelViewMatrix.PushMatrix();
    
    //大球旋转
    modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f);
    
    //指定合适的着色器(点光源着色器)
    shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF,transformPipeline.GetModelViewMatrix(),transformPipeline.GetProjectionMatrix(),vLightPos,vTorusColor);
    torusBatch.Draw();
    
    //绘制完毕出栈
    modelViewMatrix.PopMatrix();
    
    
    //画小球
    for (int i = 0; i < NUM_SPHERES; i++) {
        modelViewMatrix.PushMatrix();
        modelViewMatrix.MultMatrix(spheres[i]);
        shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF,transformPipeline.GetModelViewMatrix(),transformPipeline.GetProjectionMatrix(),vLightPos,vSphereColor);
        sphereBatch.Draw();
        modelViewMatrix.PopMatrix();
    }
    
    //让一个小球围绕大球旋转
    modelViewMatrix.Rotate(yRot * -2.0f, 0, 1, 0);
    modelViewMatrix.Translate(0.8f, 0, 0);
    
    shaderManager.UseStockShader(GLT_SHADER_FLAT,transformPipeline.GetModelViewProjectionMatrix(),vSphereColor);
    sphereBatch.Draw();
    modelViewMatrix.PopMatrix();
    
    
    glutSwapBuffers();
    
    glutPostRedisplay();
    
}


int main(int argc, char* argv[]){
    
    //设置当前工作目录,针对MAC OS X
    gltSetWorkingDirectory(argv[0]);
    
    //初始化GLUT库
    glutInit(&argc, argv);
    
    //初始化缓冲区窗口,其中标志GLUT_DOUBLE GLUT_RGBA GLUT_DEPTH GLUT_STENCIL 分别指 双缓冲区 颜色缓冲区 深度缓冲区 模板缓冲区
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
    
    glutInitWindowSize(600, 600);
    
    glutCreateWindow("BaseDemo");
    
    //注册回调函数
    glutReshapeFunc(ChangeSize);
    
    glutDisplayFunc(RenderScene);
    
    //驱动程序初始化过程中是否出现问题
    GLenum error = glewInit();
    if (GLEW_OK != error) {
        fprintf(stderr, "glew error:%s\n",glewGetErrorString(error));
        return 1;
    }
    
    SetupRC();
    
    glutMainLoop();
    
    return 0;
}

